#include "kernel/types.h"
#include "kernel/net.h"
#include "kernel/stat.h"
#include "user/user.h"

//
// send a UDP packet to the localhost (outside of qemu),
// and receive a response.
//
static void
ping(uint16 sport, uint16 dport, int attempts)
{
  int fd;
  char *obuf = "a message from xv6!a message from xv6!a message from xv6!a message from xv6!a message from xv6!a message from xv6!a message from xv6!a message from xv6!a message from xv6!a message from xv6!a message from xv6!a message from xv6!a message from xv6!a message from xv6!a message from xv6!";
  uint32 dst;

  // 10.0.2.2, which qemu remaps to the external host,
  // i.e. the machine you're running qemu on.
  dst = (10 << 24) | (0 << 16) | (2 << 8) | (2 << 0);


  // you can send a UDP packet to any Internet address
  // by using a different dst.
  
  
  // you can send a UDP packet to any Internet address
  // by using a different dst.
  
  if((fd = connect(dst, sport, dport, IPPROTO_TCP)) < 0){
    fprintf(2, "ping: connect() failed\n");
    exit(1);
  }

  for(int i = 0; i < attempts; i++) {
    if(write(fd, obuf, strlen(obuf)) < 0){
      fprintf(2, "ping: send() failed\n");
      exit(1);
    }
  }

  char ibuf[128];
  int cc = read(fd, ibuf, sizeof(ibuf)-1);
  if(cc < 0){
    fprintf(2, "ping: recv() failed\n");
    exit(1);
  }

  ibuf[cc] = '\0';
  printf("tcptests receive %s\n", ibuf);
  
}


int
main(int argc, char *argv[])
{
  uint16 dport = NET_TESTS_PORT;

  printf("tcptests running on port %d\n", dport);
  
  printf("testing ping: ");
  ping(2000, dport, 1);
  printf("OK\n");
  
  exit(0);
}
